<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/

class album_api{
	
	
	public function createDefaultAlbum($uid){
		$albumnum = $this->getalbumnumbyuid($uid);
		if(!$albumnum){
			$category = load_api('category')->getSystemCategory();
			if($category){
				$data['album_title'] = lang('default_albumtitle','default');
				$data['album_desc'] = lang('default_albumdesc','default');
				$data['category_id'] = $category['category_id'];
				$data['user_id'] = $uid;
				return $this->addAlbum($data);
			}
		}
		return false;
	}

	public function getHotestAlbum($num,$sharenum=0){
		$result = pcache('r', 'hotest_album');
		if(!$result){
			$albumdb = load_db('album');
			$setting = load_api('setting')->getSetting('layouthome');
			$order = ' ptx_album.total_like DESC ';
			if($setting['uihome_albumorder']=='likes'){
				$order = ' ptx_album.total_like DESC ';
			}elseif($setting['uihome_albumorder']=='shares'){
				$order = ' ptx_album.total_share DESC ';
			}elseif($setting['uihome_albumorder']=='uptime'){
				$order = ' ptx_album.update_time DESC ';
			}
			$result = $albumdb->search_no_page(array('total_share_num'=>$sharenum),NULL,$order,$num);
			$setting = load_api('setting')->getSetting('optimizer');
			$time = $setting['cachetime_homealbum']?$setting['cachetime_homealbum']:3600;

			foreach ($result as $key=>$val){
				$result[$key] = $this->prepareAlbum($val);
				$result[$key]['ml']=($key%4==0)?'ml0':'';
			}
			pcache('w','hotest_album',$result,$time);
		}
		return $result;
	}

	public function clearHotestAlbumCache(){
		pcache('c','hotest_album');
	}

	public function findAlbumByid($albumid){
		$album = load_db('album')->find_one(array('album_id'=>$albumid));

		return $this->prepareAlbum($album);
	}

	public function search($conditions,$page,$pagesize,$order){
		$db = load_db('album');
		return $db->search($conditions,$page,$pagesize,null,$order);
	}

	public function prepareAlbum($album){
		$covers = str_to_arr_list($album['album_cover']);
		foreach ($covers as $key=>$cover){
			$covers[$key]['image'] = thumburl($cover['image_path_0'],'square',$cover['cloud']);
		}
		$album['link']=site_url(('home/albumshare?aid='.$album['album_id']));
		$setting = load_api('setting')->getSetting('layoutalbum');
		$album_type = ($setting['uialbum_layout'])?$setting['uialbum_layout']:9;

		$tcover = count($covers);
		if($tcover<$album_type){
			for($i=$tcover;$i<$album_type;$i++){
				$covers[$i]['image'] = FALSE;
			}
		}
		if($tcover>$album_type){
			for($i=$album_type;$i<$tcover;$i++){
				unset($covers[$i]);
			}
		}
		if($album_type==6) {
			$covers[0]['class'] = 'cell_6_1';
		}
		$album['albumtype'] = $album_type;
		$album['covers'] = $covers;
		return $album;
	}

	public function getLatestFullAlbum($num){
		$result = pcache('r', 'lastest_full_album');
		if(!$result){
			$albumdb = load_db('album');
			$result = $albumdb->search_no_page(array('total_share_num'>9),NULL,' ptx_album.create_time DESC ',$num);
			$time = $this->settings['optimizer_setting']['cache_time_album'];
			$time = $time?$time:3600;
			pcache('w','lastest_full_album',$result,$time);
		}
		return $result;
	}

	public function getAlbumsByUser($uid,$category_id){
		$albumdb = load_db('album');
		$condition = array('user_id'=>$uid);
		if($category_id){
			$condition['category_id']=$category_id;
		}
		return $albumdb->findAll($condition);
	}

	public function albumcheck($postdata, $param){
		$pz = &get_instance();
		return false == load_db('album')->find(array("album_title"=>$postdata,'user_id'=>$pz->cuser['uid']));
	}

	public function addAlbum($data){
		$albumdb = load_db('album');
		$album_id = $albumdb->add_one($data);
		if($album_id){
			$data['album_id'] = $album_id;
			$data['update_time'] = TIMESTAMP;
			$userapi = load_api('user');
			$userapi->addAlbum($data['user_id']);
		}
		return $data;
	}

	public function findFavAlbumByIds($aid,$uid){
		return load_db('album/favalbum')->find(array('album_id'=>$aid,'user_id'=>$this->cuser['uid']));
	}

	public function deleteAlbum($id){
		$albumdb = load_db('album');
		if($album = $this->findAlbumByid($id)){
			if($albumdb->deleteByPk($id)){
				load_db('comment/albumcomment')->deleteBy('album_id',$id);
				load_db('album/favalbum')->deleteBy('album_id',$id);
				load_api('user')->delAlbum($album['user_id']);
				$shareapi = load_api('share');
				$shares = $shareapi->getShareByAlbum($id);
				foreach ($shares as $share){
					$shareapi->deleteShareById($share['share_id']);
				}
			}
		}
		return true;
	}

	public function deleteByUser($uid){
		$albumdb = load_db('album');
		$condition = array('user_id'=>$uid);
		$albums = $albumdb->findAll($condition);
		foreach ($albums as $album){
			$this->deleteAlbum($album['album_id']);
		}
	}

	function update_album_cover($album_id){
		$sharedb = load_db('share');
		$conditions['album_id'] = $album_id;
		$conditions['type'] = 'noarticle';
		$shares = $sharedb->search_no_page($conditions,' ptx_share.share_id,item.image_path_0,item.item_type,item.cloud ',null,9);

		$upconditions['album_id'] = $album_id;
		$data['total_share'] = $sharedb->findCount($upconditions);
		$data['album_cover'] = arr_list_to_str($shares);
		$data['update_time'] = TIMESTAMP;
		load_db('album')->update($upconditions,$data);
	}

	public function getalbumnumbyuid($uid){
		return load_db('album')->findCount(array('user_id'=>$uid));
	}
}
